/*
 * @Author: wsf
 * @Date: 2019-02-20 16:14:17
 * @LastEditors: wsf
 * @LastEditTime: 2019-08-12 14:14:47
 * @Description: 
 */
import React, { PureComponent } from "react"
import { isNull } from "../../utils/helper"
import { connect } from "dva";
@connect(({ form }, { formDataKey }) => {
  return ({
    ...form[formDataKey]
  })
})
export default class MyDatasource extends PureComponent {
  constructor(props) {
    super(props);
    this.timer = null;
    let { isSearch } = props;
    this.state = {
      show: isSearch
    }
  }

  static propTypes = {
  }

  watchStr(arr, oldFD = {}, newFD = {}) {
    let oldStr = "", newStr = "";
    if (!arr || arr.length == 0) {
      return false
    }
    else {
      arr.map(item => {
        let key = item.store_field || item.key;
        oldStr += "," + oldFD[key];
        newStr += "," + newFD[key];
      });
    }
    return oldStr !== newStr
  }
  componentWillReceiveProps(next) {
    const { id, api, source_type, dispatch, formDataKey, options } = next;
    let oldFD = this.props.formData || {};
    let newFD = next.formData || {};
    let { refreshing: oldRefreshing } = this.props;
    let { refreshing: newRefreshing } = next;
    //如果source_type==="option" 就不监听formData
    // refreshing 变化的时候也会触发刷新 
    // setTimeout(() => {
      if (source_type === "server" && ((oldRefreshing !== newRefreshing && newRefreshing) || this.watchStr(api.params, oldFD, newFD))) {
        dispatch({
          type: "form/getDataSource",
          payload: {
            options: options,
            dataset_key: id,
            apiInfo: api,
            data: next.formData || {},
            source_type,
            formDataKey
          }
        })
      }
    // }, 300);
  }
  componentDidMount() {
    let { id, api, dispatch, source_type, options, formDataKey, formData, defaultFormData, didFormData, auto_init } = this.props;
    //auto_init 界面初始化时是否加载默认1加载
    if (isNull(didFormData) || JSON.stringify(didFormData) == "{}") {
      if (isNull(formData) || JSON.stringify(formData) == "{}") {
        if (isNull(defaultFormData) || JSON.stringify(defaultFormData) == "{}") {
          didFormData = {}
        }
        else {
          didFormData = defaultFormData
        }
      }
      else {
        didFormData = formData
      }
    }
    let autoInit = true;
    try {
      if (source_type == "server" && api.page_enable == 1 && api.url.indexOf("lv/data") != -1) {
        autoInit = false
      }
    } catch (e) {
      console.log(e);
    }
    if (!isNull(auto_init) && auto_init == 0) {
      autoInit = false;
    }
    if (!autoInit) {//如果是listview 第一次就不加载 组件内部自己加载
      return;
    }
    dispatch({
      type: "form/getDataSource",
      payload: {
        options,
        apiInfo: api,
        data: didFormData || {},
        source_type,
        dataset_key: id,
        formDataKey
      }
    })
    let { auto_load, auto_load_sec } = this.props;
    auto_load_sec = isNull(auto_load_sec) ? 5 : auto_load_sec
    if (auto_load == 1 && source_type == "server") {
      clearInterval(this.timer);
      this.timer = setInterval(() => {
        dispatch({
          type: "form/getDataSource",
          payload: {
            options,
            apiInfo: api,
            data: didFormData || {},
            source_type,
            dataset_key: id,
            formDataKey
          }
        })
      }, auto_load_sec * 1000)
    }
  }
  render() {
    return null;
  }
  componentWillUnmount() {
    clearInterval(this.timer)
  }
}